package cn.wgx.module.mind.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.wgx.common.base.BaseLogger;
import cn.wgx.common.util.DateUtils;
import cn.wgx.common.util.UUIDUtils;
import cn.wgx.module.mind.entity.Srt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * .srt 字幕工具类
 */
public class SrtUtil  {

    protected static Logger logger = LoggerFactory.getLogger(SrtUtil.class);

    //GMT+8 = Asia/Hong_Kong = Asia/Shanghai
    private final static TimeZone timezone = TimeZone.getTimeZone("GMT+8");

    public static File createSrtFile(ArrayList<Srt> list, String tmpPath){
        String srt = createSrt(list);
        logger.debug("字幕内容为: {}", srt.replaceAll("\r\n", " - "));
        File file = new File(tmpPath + UUIDUtils.getGUID() + ".srt");
        if(file.exists()){
            FileUtil.del(file);
        }
        try {
            file.createNewFile();
            FileUtil.appendString(srt, file, "utf-8");
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String createSrt(ArrayList<Srt> list){
        if(list == null || list.isEmpty()){
            return null;
        }
        StringBuffer stringBuilder = new StringBuffer();
        for (Srt item : list) {
            stringBuilder.append(item.getId());
            stringBuilder.append("\r\n");
            stringBuilder.append(long2SrtTime(item.getStartTime()));
            stringBuilder.append(" --> ");
            stringBuilder.append(long2SrtTime(item.getEndTime()));
            stringBuilder.append("\r\n");
            if (StrUtil.isNotEmpty(item.getContent())) {
                stringBuilder.append(item.getContent());
                stringBuilder.append("\r\n");
            }
            stringBuilder.append("\r\n");
        }
        return stringBuilder.toString();
    }

    // 100123 毫秒秒转换成 00:01:40.123
    private static String long2SrtTime(Long millis){
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(timezone);
        cal.setTimeInMillis(millis);
        //起始时间是所以我们需要减去
        // 1970.1.1 8:0:0,000
        // 赋值时年月日时分秒常用的6个值，注意月份下标从0开始，所以取月份要+1
        // 时 - 8
//        System.out.println("年:" + (cal.get(Calendar.YEAR) -1970));
//        System.out.println("月:" + (cal.get(Calendar.MONTH) ));
//        System.out.println("日:" + (cal.get(Calendar.DAY_OF_MONTH) - 1));
//        System.out.println("时:" + (cal.get(Calendar.HOUR_OF_DAY) -8));
//        System.out.println("时:" + (cal.get(Calendar.HOUR) - 8));
//        System.out.println("分:" + cal.get(Calendar.MINUTE));
//        System.out.println("秒:" + cal.get(Calendar.SECOND));
//        System.out.println("毫秒:" + cal.get(Calendar.MILLISECOND));

        StringBuffer sb = new StringBuffer();
        sb.append((cal.get(Calendar.HOUR_OF_DAY) -8));
        sb.append(":");
        sb.append((cal.get(Calendar.MINUTE)));
        sb.append(":");
        sb.append((cal.get(Calendar.SECOND)));
        sb.append(",");
        sb.append((cal.get(Calendar.MILLISECOND)));

        return sb.toString();
    }

//    public static void main(String []a){
//        System.out.println(long2SrtTime(100123l));
//
//        System.out.println(long2SrtTime(0l));
//    }
}
